<?php

class M_shifts Extends MY_Model
{
	public function __construct()
	{
		parent::__construct();
		$this->shift = parent::$shift;
	}

	public function new_time_shifts($x)
	{
		$new_shift['shift_in'] = $x->timein;
		$new_shift['shift_out'] = $x->timeout;
		$new_shift['created'] = NOW;
		$this->db->insert($this->shift,$new_shift);
		return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
	}
	
	public function get_time_shift($id = FALSE) 
	{
		if($id == FALSE)
		{
			$query = $this->db->select('shift_in,shift_out')->get($this->shift);
			return $query->num_rows() >= 1 ? $query->result() : FALSE;
		}else{
			$query = $this->db->select('shift_in,shift_out')->where('id',$id)->get($this->shift);
			return $query->num_rows() >= 1 ? $query->row() : FALSE;
		}
	}
	
	public function get_time_shift_array()
	{
		$query = $this->db->select('id,shift_in,shift_out')->get($this->shift);
		if($query->num_rows() >=1)
		{
			$res = $query->result();
			$opt[''] = '- Select Time Shifts -';
			foreach($res as $v)
			{
				$opt[$v->id] = $v->shift_in.' - '.$v->shift_out;
			}
		}else{
			$opt[''] = '- No Time Shifts Available-';
		}
		return $opt;
	}
	
	public function add_employee_shifts($data,$id)
	{
		$this->db->set('active',0)->where('dep_id',$id)->update(parent::$shift_table);

		$this->db->trans_start();
		$this->db->insert_batch(parent::$shift_table,$data);
		$this->db->trans_complete();
		if ($this->db->trans_status() === FALSE)
		{
			return FALSE;
		}else{
			return TRUE;
		}
	}
	
	public function get_employee_time_shifts_by_dept($x)
	{
		$employees = parent::$emp_table;
		$positions = parent::$pos_table;
		$departments = parent::$dep_table;
		$shift_table = parent::$shift_table;
		$shifts = parent::$shift;
		
		$sql = "SELECT e.id as emp_id,
					   e.firstname,
					   e.lastname,
					   e.middlename,
					   e.gender,
					   e.address,
					   e.status,
					   e.dependents,
					   p.position_name as position,
					   e.monthly_salary,
					   e.created_at,
					   d.department_name as dept_name,
					   concat(ss.shift_in,' - ',ss.shift_out) as shift,
					   ss.id as shift_id
				FROM {$employees} e
				LEFT JOIN {$departments} d ON d.id = e.department
				LEFT JOIN {$positions} p ON p.id = e.position
				LEFT JOIN {$shift_table} s ON s.emp_id = e.id
				LEFT JOIN {$shifts} ss ON ss.id = s.shift_id
				WHERE e.department = ? 
				AND s.active = 1 
				AND s.dep_id = ?
				ORDER BY e.lastname ASC ";
				
		$query = $this->db->query($sql,array($x,$x));
		return $query->num_rows() >=1 ? $query->result() : FALSE;
	}
	
	public function get_employee_and_shifts($x)
	{
		$employees = parent::$emp_table;
		$positions = parent::$pos_table;
		$departments = parent::$dep_table;
		$shift_table = parent::$shift_table;
		$shifts = parent::$shift;
		
		$sql = "SELECT e.id as emp_id,
					   e.firstname,
					   e.lastname,
					   e.middlename,
					   e.gender,
					   e.address,
					   e.status,
					   e.dependents,
					   e.monthly_salary,
					   e.created_at,
					   concat(ss.shift_in,' - ',ss.shift_out) as shift,
					   ss.id as shift_id
				FROM {$employees} e
				LEFT JOIN {$shift_table} s ON s.emp_id = e.id
				LEFT JOIN {$shifts} ss ON ss.id = s.shift_id
				WHERE e.department = ? 
				ORDER BY e.lastname ASC ";
				
		$query = $this->db->query($sql,array($x));
		return $query->num_rows() >=1 ? $query->result() : FALSE;
	}










}